unit ContractsUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ImgList, ToolWin, Grids, DBGrids, DataUnit, DB, ADODB,
  rxPlacemnt, TasksUnit, frxClass, StdCtrls;

type
  TContractsForm = class(TForm)
    ToolBar1: TToolBar;
    ImageList1: TImageList;
    TasksToolButton: TToolButton;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    ADOQuery1: TADOQuery;
    FormStorage1: TFormStorage;
    AddContractToolButton: TToolButton;
    ToolButton1: TToolButton;
    IPComboBox: TComboBox;
    Label1: TLabel;
    ADOQuery2: TADOQuery;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TasksToolButtonClick(Sender: TObject);
    procedure AddContractToolButtonClick(Sender: TObject);
    procedure IPComboBoxChange(Sender: TObject);
  private
    { Private declarations }
    FPerformerId: integer;
    FContractId: integer;
    FOpenDB: boolean;

    procedure RefreshFields;
  public
    { Public declarations }
    procedure SetupForm(const aDBADOConnection: TADOConnection);
  end;

var
  ContractsForm: TContractsForm;

procedure ViewContracts(const aDBADOConnection: TADOConnection);

implementation
uses
  ContractUnit;

{$R *.dfm}

procedure TContractsForm.SetupForm(const aDBADOConnection: TADOConnection);
var
  DBPath: string;
  id: integer;
begin
  FOpenDB:= (NOT Assigned(aDBADOConnection));
  if FOpenDB then begin
    DBPath:= ExtractFilePath(ParamStr(0))+'database\akts.mdb';
    DBDataModule.OpenDB(DBPath);
  end;
  ADOQuery1.Connection:= DBDataModule.DBADOConnection;

  ADOQuery2.Connection:= DBDataModule.DBADOConnection;
  ADOQuery2.Active:= False;
  ADOQuery2.SQL.Clear;
  ADOQuery2.SQL.Add('SELECT customers.customerid, customers.customername');
  ADOQuery2.SQL.Add('FROM customers');
  ADOQuery2.SQL.Add('WHERE (customers.customertype = 1)');
  ADOQuery2.Active:= True;

  FPerformerId:= 0;

  ADOQuery2.First;
  while (not ADOQuery2.Eof) do begin
    id:= ADOQuery2.FieldByName('customerid').AsInteger;
    IPComboBox.AddItem(ADOQuery2.FieldByName('customername').AsString,TObject(id));
    ADOQuery2.Next;
  end;

 if (IPComboBox.Items.Count > 0) then begin
    IPComboBox.ItemIndex:= 0;
    FPerformerId:= Integer(IPCombobox.Items.Objects[IPCombobox.ItemIndex]);
    RefreshFields;
  end;
  
  ADOQuery2.Active:= False;
end;

procedure TContractsForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if FOpenDB then begin
    DBDataModule.CloseDB;
  end else begin
    FreeAndNil(DBDataModule);
  end;
end;

procedure TContractsForm.FormCreate(Sender: TObject);
begin
  {$IFNDEF EMPLOYER}
  SetupForm(nil);
  {$ENDIF}
end;

procedure TContractsForm.IPComboBoxChange(Sender: TObject);
begin
  if (IPComboBox.Items.Count > 0) then begin
    FPerformerId:= Integer(IPComboBox.Items.Objects[IPComboBox.ItemIndex]);
  end;
  
  RefreshFields;
end;

procedure TContractsForm.TasksToolButtonClick(Sender: TObject);
begin
  FContractId:= DBGrid1.DataSource.DataSet.FieldByName('contractid').AsInteger;
  ViewTasks(FContractId);  
end;

procedure TContractsForm.AddContractToolButtonClick(Sender: TObject);
var
  performerId: integer;
begin
  performerId:= Integer(IPComboBox.Items.Objects[IPComboBox.ItemIndex]);
  FContractId:= EditContract(performerId,0);
  if (FContractId <> 0) then begin
    ADOQuery1.Active:= False;
    ADOQuery1.Active:= True;
  end;
  RefreshFields;
end;

procedure TContractsForm.RefreshFields;
begin
  AddContractToolButton.Enabled:= True;

  if (FPerformerId <> 0) then begin
    ADOQuery1.Active:= False;
    ADOQuery1.SQL.Clear;
    ADOQuery1.SQL.Add('SELECT customers.customername, contracts.contractnum, contracts.contractdate, contracts.contractid');
    ADOQuery1.SQL.Add('FROM customers INNER JOIN contracts ON customers.customerid = contracts.customerid');
    ADOQuery1.SQL.Add('WHERE (contracts.performerid = :aPerformerId)');
    ADOQuery1.SQL.Add('ORDER BY contracts.contractdate DESC');
    ADOQuery1.Parameters.ParamByName('aPerformerId').Value:= FPerformerId;
    ADOQuery1.Active:= True;
  end;

  if (DBGrid1.DataSource.DataSet.RecordCount = 0) then begin
    AddContractToolButton.Enabled:= False;    
  end;

  if (FContractId <> 0) then begin
    DBGrid1.DataSource.DataSet.DisableControls;
    DBGrid1.DataSource.DataSet.Locate('contractid',FContractId,[loCaseInsensitive,loPartialKey]);
    DBGrid1.DataSource.DataSet.EnableControls;
  end;

  Refresh;
end;

procedure ViewContracts(const aDBADOConnection: TADOConnection);
begin
  ContractsForm:= TContractsForm.Create(Application);
  ContractsForm.SetupForm(aDBADOConnection);
  ContractsForm.ShowModal;
  FreeAndNil(ContractsForm);
end;

end.
